﻿using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FTBS.Models.DAO
{
    public class ChairTypeDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();

        /// <summary>
        /// List all Chair Types of Chair 
        /// </summary>
        /// <returns>List of chair type</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/2</date>
        public List<ChairType> LoadChairTypes()
        {
            try
            {
                List<ChairType> listChairType = new List<ChairType>();
                var chairTypes = from chairType in _entities.ChairTypes
                                 where chairType.IsDeleted == false
                                 select chairType;
                if (chairTypes != null)
                {
                    listChairType = chairTypes.ToList();
                }
                return listChairType;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Load Chair Type by Id 
        /// </summary>
        /// <param name="chairTypeId"></param>
        /// <returns>A current chair type</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/14</date>
        public ChairType SelectChairType(int chairTypeId)
        {
            try
            {
                ChairType chairType = new ChairType();
                chairType = (from chairTypeEnt in _entities.ChairTypes
                             where chairTypeEnt.ChairTypeId == chairTypeId
                             && chairTypeEnt.IsDeleted == false
                             select chairTypeEnt).FirstOrDefault();
                return chairType;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add chair type to Model and return if it successed or not
        /// </summary>
        /// <param name="cType"></param>
        /// <returns>true || false</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/2</date>
        public int CreateChairType(ChairType cType)
        {
            try
            {
                _entities.ChairTypes.Add(cType);
                int resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check if chair type name is existed or not  
        /// </summary>
        /// <param name="cType"></param>
        /// <returns>true || false</returns>
        /// <author>duyhdv</author>
        /// <date>2014/03/2</date>
        public bool CheckIsExisted(ChairType cType)
        {
            try
            {
                var isExist = _entities.ChairTypes.Any(c => c.TypeName == cType.TypeName);
                return isExist;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Edit chair type information by in put chair type id and changing information
        /// </summary>
        /// <param name="cType"></param>
        /// <returns> true|| false</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/2</date>
        public int EditChairType(ChairType cType)
        {
            try
            {
                var startingIssue = _entities.ChairTypes.Find(cType.ChairTypeId);
                _entities.Detach(startingIssue);

                _entities.Entry(cType).State = EntityState.Modified;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Romove chair type by update status isDeleted to false
        /// </summary>
        /// <param name="cTypeId"></param>
        /// <returns>1: Success -- 0: Fail</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/2</date>
        public int RemoveChairType(ChairType cType)
        {
            try
            {
                cType.IsDeleted = true;
                _entities.Entry(cType).State = EntityState.Modified;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public ChairType GetChairTypeById(int id)
        {
            var chairType = _entities.ChairTypes.Find(id);
            return chairType;
        }
    }
}